/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.*;

/**
 * @author Abahe
 */
public class Gesture extends MIDlet implements CommandListener {

    private Canvas canvas;

    public void startApp() {
        if (canvas == null) {
            canvas = new TestCanvas();
            canvas.addCommand(new Command("Exit", Command.EXIT, 0));
            canvas.setCommandListener(this);
        }
        Display.getDisplay(this).setCurrent(canvas);
    }

    public void pauseApp() {
        // empty
    }

    public void destroyApp(boolean must) {
        // empty
    }

    public void commandAction(Command c, Displayable d) {
        if (c.getCommandType() == Command.EXIT) {
            notifyDestroyed();
        }
    }

    private static class TestCanvas extends Canvas {
        private GestureRecognizer recognizer;
        private int gesture;
        private boolean show;

        protected void paint(Graphics g) {
            g.setColor(0x400000);
            g.fillRect(0, 0, getWidth(), getHeight());

            if (show) {
                g.setColor(0xffff00);
                String s;
                boolean drawTap = false;
                boolean drawArea = false;

                switch (gesture) {
                case GestureRecognizer.TAP:
                    s = "tap";
                    drawTap = true;
                    break;
                case GestureRecognizer.TAP_N_HOLD:
                    s = "tap'n'hold";
                    drawTap = true;
                    break;
                case GestureRecognizer.DRAG_UP:
                    s = "drag up";
                    break;
                case GestureRecognizer.DRAG_DOWN:
                    s = "drag down";
                    break;
                case GestureRecognizer.DRAG_LEFT:
                    s = "drag left";
                    break;
                case GestureRecognizer.DRAG_RIGHT:
                    s = "drag right";
                    break;
                case GestureRecognizer.CIRCLE_CLOCK:
                    s = "circle clockwise";
                    drawArea = true;
                    break;
                case GestureRecognizer.CIRCLE_ANTI:
                    s = "circle anti-clockwise";
                    drawArea = true;
                    break;
                default:
                    s = "unknown";
                    break;
                }

                g.drawString(s, getWidth() / 2, getHeight(), Graphics.HCENTER | Graphics.BOTTOM);

                if (drawTap) {
                    g.drawRect(recognizer.getTapX() - 4, recognizer.getTapY() - 4, 8, 8);
                }
                if (drawArea) {
                    g.drawRect(recognizer.getAreaX(), recognizer.getAreaY(), recognizer.getAreaWidth(), recognizer.getAreaHeight());
                }
            }
        }

        protected void pointerPressed(int x, int y) {
            recognizer = new GestureRecognizer(x, y);

            show = false;
            repaint();
        }

        protected void pointerDragged(int x, int y) {
            recognizer.addMotion(x, y);
        }

        protected void pointerReleased(int x, int y) {
            recognizer.endGesture(x, y);
            gesture = recognizer.getGesture();

            show = true;
            repaint();
        }
    }
}
